package package1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;
import java.util.regex.Pattern;
 
 
public class homework1 {
	
	public List<String> list;
	 public homework1(String input) {
	        char[] ins = input.toCharArray();
	        list = new ArrayList<String>();
	        String str = "";
	        
	        for (int i = 0; i < ins.length; i++) {
	            //为负数的情况,如-1+2,  2*(-1+2)
	            if (ins[i] == '-' && ins[i + 1] >= '0' && ins[i + 1] <= '9'
	                    && (i == 0 || ins[i - 1] == '(')) {
	                str += ins[i];
	                continue;
	            }
	            //为()-()的情况
	            if (ins[i] == '-' && ins[i + 1] == '('&&(i==0||ins[i-1]!=')')) {
	                list.add("0");
	                list.add("-");
	                list.add("1");
	                list.add("*");
	                continue;
	            }
	            if (!checkFuhao(ins[i])) {
	                str += ins[i];
	                if (i == ins.length - 1) {
	                    list.add(str);
	                }
	            } else {
	                if (!"".equals(str)) {
	                    list.add(str);
	                    str = "";
	                }
	                list.add(ins[i] + "");
	            }
	            //判断2()的情况
	            if (i>0&&ins[i]=='('&&ins[i-1]>='0'&&ins[i-1]<='9') {
	                list.add(list.size()-1,"*");
	            }
	        }

	    }
	     
	    /**主方法,通过迭代的形式,输入经过分解后的集合
	     * @param list
	     * @return 运算结果
	     */
	    private String yunsuan(List<String> list) {
	        try {
	            int zuo = haveZuoKuoHao(list);
	            if (zuo != -1) {
	                int you = lastYouKuohao(list, zuo);
	                List<String> l = removeKuohao(list, zuo, you);
	                addKuohao(list, yunsuan(l), zuo);
	                yunsuan(list);
	            }
	            int chengchu = haveChengChu(list);
	            if (chengchu != -1) {
	                chengchuYunsuan(list, chengchu);
	            } else {
	                jiajianYunsuan(list);
	            }
	            if (list.size() == 1) {
	                return list.get(0);
	            }
	        } catch (Exception e) {
	            System.out.println("error");
	            System.exit(0);
	        }
	        return yunsuan(list);
	    }
	     
	    /**去括号后替换原括号为括号运算后的值
	     * @param list
	     * @param yunsuan
	     * @param zuo
	     */
	    private void addKuohao(List<String> list, String yunsuan, int zuo) {
	        list.add(zuo, yunsuan);
	 
	    }
	     
	    /**当最后只有+-符号后,进行运算
	     * @param list
	     */
	    private void jiajianYunsuan(List<String> list) {
	        for (int i = 0; i < list.size(); i++) {
	            if (list.get(i).equals("+")) {
	                double last = Double.parseDouble(list.remove(i + 1));
	                list.remove(i);
	                double first = Double.parseDouble(list.get(i - 1));
	                list.set(i - 1, last + first + "");
	                i--;
	            }
	            if (list.get(i).equals("-")) {
	                double last = Double.parseDouble(list.remove(i + 1));
	                list.remove(i);
	                double first = Double.parseDouble(list.get(i - 1));
	                list.set(i - 1, first - last + "");
	                i--;
	            }
	        }
	 
	    }
	     
	    /**进行乘除运算
	     * @param list
	     * @param chengchu
	     */
	    private void chengchuYunsuan(List<String> list, int chengchu) {
	        double last = Double.parseDouble(list.remove(chengchu + 1));
	        String fuhao = list.remove(chengchu);
	        double first = Double.parseDouble(list.get(chengchu - 1));
	        if (fuhao.equals("*")) {
	            list.set(chengchu - 1, first * last + "");
	        }
	        if (fuhao.equals("/")) {
	            list.set(chengchu - 1, first / last + "");
	        }
	 
	    }
	     
	    /**判断算式中是否有乘除
	     * @param list
	     * @return 乘除符号的位置
	     */
	    private int haveChengChu(List<String> list) {
	        for (int i = 0; i < list.size(); i++) {
	            if (list.get(i).equals("*") || list.get(i).equals("/")) {
	                return i;
	            }
	        }
	        return -1;
	    }
	 
	    /**将括号移除,并把括号内的算式重新记为一个算式
	     * @param list
	     * @param zuo
	     * @param you
	     * @return 括号内的算式
	     */
	    private List<String> removeKuohao(List<String> list, int zuo, int you) {
	        List<String> l = new ArrayList<String>();
	        list.remove(you);
	        list.remove(zuo);
	        for (int i = zuo; i <= you - 2; i++) {
	            l.add(list.remove(i));
	            i--;
	            you--;
	        }
	        return l;
	    }
	 
	    /** 如果有左括号
	     * @param list
	     * @return 左括号的位置
	     */
	    private int haveZuoKuoHao(List<String> list) {
	        for (int i = 0; i < list.size(); i++) {
	            if (list.get(i).equals("(")) {
	                return i;
	            }
	        }
	        return -1;
	    }
	 
	    /**某个左括号所对应的右括号
	     * @param list
	     * @param zuo
	     * @return 这个右括号的位置
	     */
	    private int lastYouKuohao(List<String> list, int zuo) {
	        int zuoIndex = 1;
	        int youIndex = 0;
	        for (int i = zuo + 1; i < list.size(); i++) {
	            if (list.get(i).equals("(")) {
	                zuoIndex++;
	            }
	            if (list.get(i).equals(")")) {
	                youIndex++;
	            }
	            if (zuoIndex==youIndex) {
	                return i;
	            }
	        }
	        return -1;
	    }
	     
	    /** 判断是否为符号
	     * @param a
	     * @return
	     */
	    private boolean checkFuhao(char a) {
	        if (a == '+' || a == '-' || a == '*' || a == '/' || a == '('
	                || a == ')') {
	            return true;
	        }
	        return false;
	    }
	 
	    private void output(List<String> list) {
	        for (int i = 0; i < list.size(); i++) {
	            System.out.print(list.get(i));
	        }
	        System.out.println();
	    }
	
	    
	    
    public static void main(String[] args){
    	
        ArrayList<String>result=new ArrayList<String>();
        Scanner in1=new Scanner(System.in);
        
        try{
        	System.out.println("请输入要生成的算式个数:");
        	int num=0;
        	int corr=0;
        	num=in1.nextInt();
        	for(int i=0;i<num;i++){
        	corr+=cre();
        	}
        	
        	
        	System.out.println("您一共做对了"+corr+"道题!");
        	
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public static int cre() throws Exception{
    	Scanner in=new Scanner(System.in);  	
    	int[] num=new int[4];
    	int[] a=new int[4];
    	char[] operator=new char[4];
    	operator[0]='+';
    	operator[1]='-';
    	operator[2]='*';
    	operator[3]='/';
    	for(int i=0;i<4;i++)
    	{
    		num[i]=(int)(Math.random()*10);
    		Random rand=new Random();
    		if(i!=1)a[i]=rand.nextInt(4);
    		else a[i]=rand.nextInt(2);
    	}    	
    	String rstring=num[0]+""+operator[a[0]]+""+num[1]+""+operator[a[1]]+""+num[2]+""+operator[a[2]]+""+num[3];
    	System.out.println(rstring);
    	System.out.println("结果:");
    	
    	String s1=in.next();
    	int correct=0;
    	test0305_1 s = new test0305_1(rstring);
        List<String> list = s.list;
        System.out.println("正确答案:"+s.yunsuan(list));
		if(s1.equalsIgnoreCase(s.yunsuan(list)))
		{
			System.out.println("正确!");
			correct=correct+1;
		}
		else System.out.println("错误!");
		//System.out.println(correct);
    	return correct;

    }        
}
   